package com.GlobalPaint.app.ui.HTML;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.util.LruCache;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.ui.Login.Login;
import com.GlobalPaint.app.utils.CommUtility;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.blankj.utilcode.utils.SPUtils;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.WebBackForwardList;
import com.tencent.smtt.sdk.WebHistoryItem;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

public class YonggDetailsActivity extends AppCompatActivity {
    private ProgressDialog progressDialog;
    private WebView details_commom;
    private String fragment;
    private String companyname;
    private String Location;
    private String money;
    private String publish;
    private String qq;
    private String telno;
    private String title;
    private String wx;
    private String memo;
    private String content;
    private String begintime;
    private String endtime;
    private int hireInfoid;
    private int state;
    private int creatId;
    private int userid;
    private int id;
    public static LocalBroadcastManager localBroadcastManager;//注册广播
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yongg_details);
        localBroadcastManager=LocalBroadcastManager.getInstance(YonggDetailsActivity.this);//注册广播
        details_commom = (WebView) findViewById(R.id.details_commom);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);

            getWindow().setFormat(PixelFormat.TRANSLUCENT);  //（这个对宿主没什么影响，建议声明）
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        }
        fragment=getIntent().getStringExtra("fragment");
        if(fragment.equals("recruit")){  //招工
            companyname=getIntent().getStringExtra("companyname");
            Location=getIntent().getStringExtra("Location");
            money=getIntent().getStringExtra("money");
            publish=getIntent().getStringExtra("publish");
            qq=getIntent().getStringExtra("qq");
            telno=getIntent().getStringExtra("telno");
            title=getIntent().getStringExtra("title");
            wx=getIntent().getStringExtra("wx");
            memo=getIntent().getStringExtra("memo");
            content=getIntent().getStringExtra("content");
            begintime=getIntent().getStringExtra("begintime");
            endtime=getIntent().getStringExtra("endtime");
            creatId=getIntent().getIntExtra("creatId",0);
        }else if(fragment.equals("jobWanted")){
            Location=getIntent().getStringExtra("Location");
            money=getIntent().getStringExtra("money");
            publish=getIntent().getStringExtra("publish");
            qq=getIntent().getStringExtra("qq");
            telno=getIntent().getStringExtra("telno");
            title=getIntent().getStringExtra("title");
            wx=getIntent().getStringExtra("wx");
            memo=getIntent().getStringExtra("memo");
            content=getIntent().getStringExtra("content");
            creatId=getIntent().getIntExtra("creatId",0);
            begintime=getIntent().getStringExtra("begintime");
            endtime=getIntent().getStringExtra("endtime");
        }else if(fragment.equals("linshi")){
            money=getIntent().getStringExtra("money");
            companyname=getIntent().getStringExtra("companyname");
            publish=getIntent().getStringExtra("publish");
            qq=getIntent().getStringExtra("qq");
            telno=getIntent().getStringExtra("telno");
            title=getIntent().getStringExtra("title");
            wx=getIntent().getStringExtra("wx");
            memo=getIntent().getStringExtra("memo");
            content=getIntent().getStringExtra("content");
            begintime=getIntent().getStringExtra("begintime");
            endtime=getIntent().getStringExtra("endtime");
            hireInfoid=getIntent().getIntExtra("hireInfoid",0);
            Location=getIntent().getStringExtra("Location");
            state=getIntent().getIntExtra("state",0);
            creatId=getIntent().getIntExtra("creatId",0);
        }else if(fragment.equals("mylinshi")){
            companyname=getIntent().getStringExtra("companyname");
            money=getIntent().getStringExtra("money");
            publish=getIntent().getStringExtra("publish");
            qq=getIntent().getStringExtra("qq");
            telno=getIntent().getStringExtra("telno");
            title=getIntent().getStringExtra("title");
            wx=getIntent().getStringExtra("wx");
            memo=getIntent().getStringExtra("memo");
            content=getIntent().getStringExtra("content");
            begintime=getIntent().getStringExtra("begintime");
            endtime=getIntent().getStringExtra("endtime");
            hireInfoid=getIntent().getIntExtra("hireInfoid",0);
            Location=getIntent().getStringExtra("Location");
            userid=getIntent().getIntExtra("userid",0);
        }else if(fragment.equals("myrecruit")){  //my招工
            companyname=getIntent().getStringExtra("companyname");
            Location=getIntent().getStringExtra("Location");
            money=getIntent().getStringExtra("money");
            publish=getIntent().getStringExtra("publish");
            qq=getIntent().getStringExtra("qq");
            telno=getIntent().getStringExtra("telno");
            title=getIntent().getStringExtra("title");
            wx=getIntent().getStringExtra("wx");
            memo=getIntent().getStringExtra("memo");
            content=getIntent().getStringExtra("content");
            begintime=getIntent().getStringExtra("begintime");
            endtime=getIntent().getStringExtra("endtime");
            userid=getIntent().getIntExtra("userid",0);
            id=getIntent().getIntExtra("id",0);
        }else if(fragment.equals("myjobWanted")){ //
            Location=getIntent().getStringExtra("Location");
            money=getIntent().getStringExtra("money");
            publish=getIntent().getStringExtra("publish");
            qq=getIntent().getStringExtra("qq");
            telno=getIntent().getStringExtra("telno");
            title=getIntent().getStringExtra("title");
            wx=getIntent().getStringExtra("wx");
            memo=getIntent().getStringExtra("memo");
            content=getIntent().getStringExtra("content");
            userid=getIntent().getIntExtra("userid",0);
            id=getIntent().getIntExtra("position",0);
            begintime=getIntent().getStringExtra("begintime");
            endtime=getIntent().getStringExtra("endtime");
        }

        progressDialog = CommUtility.openProgressDialog(YonggDetailsActivity.this,
                progressDialog, "正在加载中...");
        details_commom.setWebViewClient(new WebViewClient() {

            @Override
            public void onLoadResource(WebView view, String url) {

                if (url.equals("phone://outofdate")) {
                    SPUtils spUtils = new SPUtils(DataManager.context, "login");
                    spUtils.remove("user");
                    //退出时清除缓存
                    DataManager.lruCache = new LruCache<>(10);
                    Intent intent = new Intent(YonggDetailsActivity.this, Login.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }
            }


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //判断url拦截事件
                if (url.indexOf("append")!=-1) {
                    finish();
                    details_commom.clearHistory(); // 清除
                    return true;
                }else if(url.indexOf("refresh")!=-1){ //接单取消接单
                    //发送广播 刷新好友列表
                    Intent intent = new Intent("refresh");
                    localBroadcastManager.sendBroadcast(intent);
                    finish();
                    return true;
                } else if (url.indexOf("appLoginOut")!=-1) {
                    SPUtils spUtils = new SPUtils(DataManager.context, "login");
                    spUtils.remove("user");
                    //退出时清除缓存
                    DataManager.lruCache = new LruCache<>(10);
                    Intent intent = new Intent(YonggDetailsActivity.this, Login.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                    return true;
                } else {
                    CommUtility.closeProgressDialog(progressDialog);
                    details_commom.loadUrl(url);
                    return false;
                }
            }

            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                CommUtility.closeProgressDialog(progressDialog);
            }

            //接收不到404
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                CommUtility.closeProgressDialog(progressDialog);
            }
        });

        WebSettings webSettings = details_commom.getSettings();
        webSettings.setJavaScriptEnabled(true);
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().sync();
        if(fragment.equals("recruit")){ //招工
            details_commom.loadUrl(Constants.JASON_SERVICE_URL_H5+"zg_detail.html?companyname="+companyname
                    +"&Location="+Location+"&money="+money+"&publish="+publish+"&qq="+qq+
                    "&telno="+telno+"&title="+title+"&wx="+wx+"&memo="+memo+"&content="+content+"&token="+ DataManager.userEntity.getCookie()
                    +"&useName="+DataManager.userEntity.getUserName()+"&type=yongGong"+"&begintime="+
                    begintime+"&endtime="+endtime+"&creatId="+creatId+"&userid="+DataManager.userEntity.getUserId());
        } else if(fragment.equals("jobWanted")) {
            details_commom.loadUrl(Constants.JASON_SERVICE_URL_H5+"qz_detail.html?money="+money
                    +"&publish="+publish+"&qq="+qq+"&telno="+telno+
                    "&title="+title+"&wx="+wx+"&memo="+memo +"&content="+content+"&token="+DataManager.userEntity.getCookie()
                    +"&useName="+DataManager.userEntity.getUserName()+"&creatId="+creatId+"&Location="+Location
                    +"&begintime="+ begintime+"&endtime="+endtime+"&userid="+DataManager.userEntity.getUserId());
        }else if(fragment.equals("linshi")) {
            details_commom.loadUrl(Constants.JASON_SERVICE_URL_H5+"zg_detail.html?money="+money+"&Location="+Location
                    +"&publish="+publish+"&companyname="+companyname+"&qq="+qq+"&telno="+telno+
                    "&title="+title+"&wx="+wx+"&memo="+memo +"&content="+content+"&token="+DataManager.userEntity.getCookie()
                    +"&useName="+DataManager.userEntity.getUserName()+"&type=linShiYongGong"+"&begintime="+
                    begintime+"&endtime="+endtime+"&hireInfoid="+hireInfoid+"&userid="+DataManager.userEntity.getUserId()+
            "&state="+state+"&creatId="+creatId);
        }else if(fragment.equals("mylinshi")) {
            details_commom.loadUrl(Constants.JASON_SERVICE_URL_H5+"zg_detail.html?money="+money+"&Location="+Location
                    +"&publish="+publish+"&qq="+qq+"&telno="+telno+"&companyname="+companyname+
                    "&title="+title+"&wx="+wx+"&memo="+memo +"&content="+content+"&token="+DataManager.userEntity.getCookie()
                    +"&useName="+DataManager.userEntity.getUserName()+"&type=myLinShiYongGong"+"&begintime="+
                    begintime+"&endtime="+endtime+"&hireInfoid="+hireInfoid+"&userid="+DataManager.userEntity.getUserId());
        }else if(fragment.equals("myrecruit")){ //my招工
            details_commom.loadUrl(Constants.JASON_SERVICE_URL_H5+"zg_detail.html?companyname="+companyname
                    +"&Location="+Location+"&money="+money+"&publish="+publish+"&qq="+qq+
                    "&telno="+telno+"&title="+title+"&wx="+wx+"&memo="+memo+"&content="+content+"&token="+ DataManager.userEntity.getCookie()
                    +"&useName="+DataManager.userEntity.getUserName()+"&type=yongGong"+"&begintime="+
                    begintime+"&endtime="+endtime+"&userid="+DataManager.userEntity.getUserId()+"&id="+id);
        }else if(fragment.equals("myjobWanted")) {
            details_commom.loadUrl(Constants.JASON_SERVICE_URL_H5 + "zg_detail.html?money=" + money
                    + "&publish=" + publish + "&qq=" + qq + "&telno=" + telno +
                    "&title=" + title + "&wx=" + wx + "&memo=" + memo + "&content=" + content + "&token=" + DataManager.userEntity.getCookie()
                    + "&useName=" + DataManager.userEntity.getUserName() + "&userid=" +DataManager.userEntity.getUserId()+"&id="+id+"&Location="+Location
                    +"&begintime="+ begintime+"&endtime="+endtime);
        }

    }


    private boolean canGoBack() {
        if (details_commom.canGoBack()) {
            WebBackForwardList list = details_commom.copyBackForwardList();
            WebHistoryItem item = list.getCurrentItem();
            System.out.println("URL: " + item.getUrl());

            if (item.getUrl().indexOf("succeed.html") >= 0) {
                finish();
                return false;
            } else if (item.getUrl().indexOf("MyLeaveDetail.html") >= 0) {
                finish();
                return false;
            }
            return details_commom.canGoBack();

        }
        finish();
        return details_commom.canGoBack();
    }

    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if (canGoBack()) {
            details_commom.goBack();
        } else {
            //super.onBackPressed();
            exit();
            details_commom.clearHistory(); // 清除
        }
    }

    private long mExitTime;

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            //     Toast.makeText(AskForLeaveActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }
}
